package com.example.my.agiftthat.ui.list.intentlist;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.example.my.agiftthat.R;
import com.example.my.agiftthat.ui.list.intentlist.fragmentreview.FragmentTreView;
import com.example.my.agiftthat.ui.list.intentlist.fragmentsuk.FragmentSUK;
import com.example.my.agiftthat.ui.list.intentlist.framgnetdetails.FragmentDeaTails;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ListIntentActivity extends AppCompatActivity {

    @Bind(R.id.intent_tabLayout)
    TabLayout intentTabLayout;
    @Bind(R.id.intent_viewPager)
    ViewPager intentViewPager;
    private String[] title;
    private List<Fragment> list;
    private Context mContext = this;
    private int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_intent);
        ButterKnife.bind(this);
        Bundle bundle1 = getIntent().getExtras();
        id = bundle1.getInt("id");
        //获取数组
        title = getResources().getStringArray(R.array.intentTitle);
        //设置title
        initTitle();
        //将碎片创建放入一个集合中
        fragments();

        //创建适配器
        intentViewPager.setOffscreenPageLimit(2);
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(),list);
        intentViewPager.setAdapter(adapter);
        intentViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(intentTabLayout));
        intentTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                intentViewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

    private void fragments() {
        list = new ArrayList<>();
        FragmentSUK fragment1 = new FragmentSUK();
        Bundle bundle = new Bundle();
        bundle.putInt("index",id);
        fragment1.setArguments(bundle);
        list.add(fragment1);
        FragmentDeaTails fragment2 = new FragmentDeaTails();
        list.add(fragment2);
        FragmentTreView fragment3 = new FragmentTreView();
        fragment3.setArguments(bundle);
        list.add(fragment3);

    }

    private void initTitle() {
        for (int i = 0; i < title.length; i++) {
            TabLayout.Tab tab = intentTabLayout.newTab();
            tab.setText(title[i]);
            intentTabLayout.addTab(tab);
        }
    }

    public void clickView(View view) {
        ListIntentActivity.this.finish();
    }
}
